/*
 * @Date: 2023-02-27 12:58:13
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-02-27 15:48:15
 * @FilePath: \A19\backend\src\api\Get\ApiGetQuestions.ts
 */
import { ApiCall } from "tsrpc";
import { ReqGetQuestions, ResGetQuestions } from "../../shared/protocols/Get/PtlGetQuestions";
import { Global } from '../../Global/index';
import { Filter, WithId,OptionalId } from "mongodb";
import { questions } from '../../shared/types/Question';

export default async function (call: ApiCall<ReqGetQuestions, ResGetQuestions>) {
    // TODO
    const teacher = call.currentUser
    const type = call.req.type
    const keyword = call.req.keyword

    const constrains:Filter<WithId<OptionalId<questions>>>[] = [
        {
            teacher:teacher._id
        }
    ]
    if(type){
        constrains.push({
            'question.type':type
        })
    }
    if(keyword){
        constrains.push({
            'question.question':new RegExp(keyword) 
        })
    }
    console.log(constrains);
    const questions = await Global.collection('questions').find({
        $and:constrains
    }).toArray()

    call.succ({
        questions
    })
}